<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar :title="`总共(${commentReplyList.length})条评论`" :fixed="true" left-arrow @click-left="$router.back()" />
    <div class="titleBox">
      <reply-item :obj="replyComment" :showKey='2'></reply-item>
      <div
      class="relist"
      :class="commentShow ? 'art-cmt-container-2' : 'art-cmt-container-1'"
      >
        <div class="listTitle">全部回复</div>
        <reply-item
          v-for="item in commentReplyList"
          :key="item.com_id"
          :obj="item"
          :showKey='1'
        ></reply-item>
      </div>
    </div>
    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="commentShow">
      <van-icon name="arrow-left" size="0.48rem" @click="leftFn" />
      <div class="ipt-cmt-div" @click="commentShow = false">发表评论</div>
      <div class="icon-box">
        <van-badge>
          <van-icon name="comment-o" size="0.53333334rem" />
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        placeholder="友善评论、理性发言、阳光心灵"
        v-model="commentText"
        v-inp
        @blur="bulrFn"
      ></textarea>
      <van-button
        type="default"
        :disabled="commentText.length === 0"
        @click="clickFn"
        >发布</van-button
      >
    </div>
  </div>
</template>

<script>
import { getCommentsAPI, releaseAPI } from '@/api'
import { mapState } from 'vuex'
import ReplyItem from './ReplyItem.vue'
export default {
  components: {
    ReplyItem
  },
  computed: {
    ...mapState('reply', ['replyComment', 'artId'])
  },
  async created () {
    const res = await getCommentsAPI({
      type: 'c',
      source: this.replyComment.com_id
    })
    this.commentReplyList = res.data.data.results
  },
  data () {
    return {
      commentShow: true,
      commentText: '',
      commentReplyList: []
    }
  },
  methods: {
    leftFn () {
      this.$emit('changeShow', false)
    },
    bulrFn () {
      // 问题：先失去焦点了, 而且你用v-if, DOM移除, 点击事件未来的及触发
      // 解决方案：失去焦点, 延时执行
      setTimeout(() => {
        this.commentShow = true
      }, 0)
    },
    async clickFn () {
      const res = await releaseAPI({
        target: this.replyComment.com_id,
        content: this.commentText,
        artId: this.artId
      })
      this.commentReplyList.unshift(res.data.data.new_obj)
      this.commentText = ''
    }
  }
}
</script>

<style lang="less" scoped>
.relist{
  padding: 10px;
}
.titleBox {
  margin-top: 46px;
}
.listTitle {
  font-size: 15px;
  padding: 16px;
}
.van-divider {
  margin: 0;
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
